package cn.enjoy.redisson.config;

import cn.enjoy.redisson.props.RedissonProperties;
import cn.enjoy.redisson.util.RedissonUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author K
 * @date 2021/7/1 下午4:34
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonConfig {

    private static List<RedissonClient> redissonClientList = new ArrayList<>();


    /**
     * 注入RedissonClient
     */
    @Bean(destroyMethod = "shutdown")
    @ConditionalOnMissingBean
    public RedissonClient redissonClient(RedissonProperties redissonProperties) {
        Config config = new Config();

        if (redissonProperties.getMasterName() != null) {
            // 哨兵
            SentinelServersConfig serverConfig = config.useSentinelServers()
                    .addSentinelAddress(redissonProperties.getSentinelAddresses())
                    .setDatabase(redissonProperties.getDatabase())
                    .setMasterName(redissonProperties.getMasterName())
                    .setTimeout(redissonProperties.getTimeout())
                    .setMasterConnectionPoolSize(redissonProperties.getMasterConnectionPoolSize())
                    .setSlaveConnectionPoolSize(redissonProperties.getSlaveConnectionPoolSize())
                    .setMasterConnectionMinimumIdleSize(redissonProperties.getMasterConnectionMinimumIdleSize())
                    .setSlaveConnectionMinimumIdleSize(redissonProperties.getSlaveConnectionMinimumIdleSize())
                    // 不检查哨兵节点个数
                    .setCheckSentinelsList(false);

            if (StrUtil.isNotBlank(redissonProperties.getPassword())) {
                serverConfig.setPassword(redissonProperties.getPassword());
            }
        } else if (redissonProperties.getMasterAddress() != null){
            // 主从
            MasterSlaveServersConfig serverConfig = config.useMasterSlaveServers()
                    //可以用"rediss://"来启用SSL连接
                    .setMasterAddress(redissonProperties.getMasterAddress())
                    .addSlaveAddress(redissonProperties.getSlaveAddress())
                    .setDatabase(redissonProperties.getDatabase())
                    .setTimeout(redissonProperties.getTimeout())
                    .setMasterConnectionPoolSize(redissonProperties.getMasterConnectionPoolSize())
                    .setSlaveConnectionPoolSize(redissonProperties.getSlaveConnectionPoolSize())
                    .setMasterConnectionMinimumIdleSize(redissonProperties.getMasterConnectionMinimumIdleSize())
                    .setSlaveConnectionMinimumIdleSize(redissonProperties.getSlaveConnectionMinimumIdleSize())
                    ;
            if (StrUtil.isNotBlank(redissonProperties.getPassword())) {
                serverConfig.setPassword(redissonProperties.getPassword());
            }
        }else {
            // 单机模式
            SingleServerConfig serverConfig = config.useSingleServer()
                    .setAddress(redissonProperties.getAddress())
                    .setDatabase(redissonProperties.getDatabase())
                    .setTimeout(redissonProperties.getTimeout())
                    .setConnectionPoolSize(redissonProperties.getConnectionPoolSize())
                    .setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize());

            if (StrUtil.isNotBlank(redissonProperties.getPassword())) {
                serverConfig.setPassword(redissonProperties.getPassword());
            }
        }

        //配置json存储，便于明文查看。
        //不管是否配置json存储，如果缓存的类路径变动，都会导致反序列化失败，必须删除旧缓存
        config.setCodec(new JsonJacksonCodec());

        return Redisson.create(config);
    }

    @Bean
    @ConditionalOnBean(RedissonClient.class)
    public RedissonUtil redissonUtil(RedissonClient redissonClient, RedissonProperties redissonProperties) {
//        redisClient(redissonProperties);
        return new RedissonUtil(redissonClient);
    }

//    private void redisClient(RedissonProperties redissonProperties) {
//        List<RedisClient> client = redissonProperties.getClient();
//        if (!CollectionUtils.isEmpty(client)) {
//            for (RedisClient redisClient : client) {
//                Config config = new Config();
//                SingleServerConfig singleServerConfig = config.useSingleServer();
//                singleServerConfig.setAddress(redisClient.getAddress())
//                        .setDatabase(redisClient.getDatabase());
//                if (StringUtil.isNotBlank(redisClient.getPassword())) {
//                    singleServerConfig.setPassword(redisClient.getPassword());
//                }
//                RedissonClient redissonClient = Redisson.create(config);
//                redissonClientList.add(redissonClient);
//            }
//        }
//    }

}
